package com.cmcc.linlaker.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cmcc.linlaker.LoginActivity;
import com.cmcc.linlaker.MainActivity;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.application.LinhuApp;
import com.cmcc.linlaker.bean.BaseBean;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.http.ConfigManager;
import com.cmcc.linlaker.ui.mine.AboutUsActivity;
import com.cmcc.linlaker.ui.mine.BookActivity;
import com.cmcc.linlaker.ui.mine.CheckInfoActivity;
import com.cmcc.linlaker.ui.mine.MessageActivity;
import com.cmcc.linlaker.ui.mine.ModifyPwdActivity;
import com.cmcc.linlaker.ui.mine.bean.UserBean;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.utils.SPHelper;
import com.cmcc.linlaker.utils.ToastUtils;
import com.cmcc.linlaker.view.ExitDialog;
import com.cmcc.linlaker.volley.GsonRequestHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.HashMap;
import java.util.Map;

public class MineFragment extends Fragment implements View.OnClickListener {
    private View fragmentView;
    public DisplayImageOptions options;

    public MineFragment() {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentView = inflater.inflate(R.layout.fragment_mine, container, false);


        BitmapFactory.Options decodingOptions = new BitmapFactory.Options();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.image_default)
                .showImageForEmptyUri(R.mipmap.image_default)
                .showImageOnFail(R.mipmap.image_default)
                .decodingOptions(decodingOptions)
                .cacheInMemory(false)
                .cacheOnDisk(false)
                .considerExifParams(true)
                .imageScaleType(ImageScaleType.NONE)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .build();

        initView();

        return fragmentView;
    }

    private TextView text_message_num;
    private ImageView user_avatar;
    private TextView textName;
    private RelativeLayout layout_modify_pwd, layout_about_us, layout_book, layout_check_info, layout_msg;
    private TextView textLogout;

    private void initView() {
        text_message_num = fragmentView.findViewById(R.id.text_message_num);
        user_avatar = fragmentView.findViewById(R.id.user_avatar);
        textName = fragmentView.findViewById(R.id.textName);

        layout_modify_pwd = fragmentView.findViewById(R.id.layout_modify_pwd);
        layout_about_us = fragmentView.findViewById(R.id.layout_about_us);
        textLogout = fragmentView.findViewById(R.id.textLogout);
        layout_book = fragmentView.findViewById(R.id.layout_book);
        layout_check_info = fragmentView.findViewById(R.id.layout_check_info);
        layout_msg = fragmentView.findViewById(R.id.layout_msg);

        layout_msg.setOnClickListener(this);
        layout_book.setOnClickListener(this);
        layout_check_info.setOnClickListener(this);
        layout_modify_pwd.setOnClickListener(this);
        layout_about_us.setOnClickListener(this);
        textLogout.setOnClickListener(this);

        getUserDetail();

        if (ConfigManager.loginData != null) {
            if (ConfigManager.loginData.messageCount != null) {
                if (!TextUtils.isEmpty(ConfigManager.loginData.messageCount.specialTotal)) {
                    if (!ConfigManager.loginData.messageCount.specialTotal.equals("0")) {
                        text_message_num.setVisibility(View.VISIBLE);
                        text_message_num.setText(ConfigManager.loginData.messageCount.specialTotal);
                    } else {
                        text_message_num.setVisibility(View.GONE);
                    }
                } else {
                    text_message_num.setVisibility(View.GONE);
                }
            } else {
                text_message_num.setVisibility(View.GONE);
            }

        } else {
            text_message_num.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_modify_pwd:
                skipToModifyPwd();
                break;
            case R.id.layout_about_us:
                skipToAboutUs();
                break;
            case R.id.layout_check_info:
                skipToCheckInfo();
                break;
            case R.id.layout_book:
                skipToBook();
                break;
            case R.id.layout_msg:
                skipToMessage();
                break;
            case R.id.textLogout:
                logout();
                break;
        }
    }

    private void skipToMessage() {
        Intent intent = new Intent(getActivity(), MessageActivity.class);
        startActivity(intent);
        text_message_num.setVisibility(View.GONE);
    }

    private void skipToCheckInfo() {
        Intent intent = new Intent(getActivity(), CheckInfoActivity.class);
        startActivity(intent);
    }

    private void skipToBook() {
        Intent intent = new Intent(getActivity(), BookActivity.class);
        startActivity(intent);
    }

    private void skipToAboutUs() {
        Intent intent = new Intent(getActivity(), AboutUsActivity.class);
        startActivity(intent);
    }

    private void skipToModifyPwd() {
        Intent intent = new Intent(getActivity(), ModifyPwdActivity.class);
        startActivity(intent);
    }

    private void logout() {
        ExitDialog dialog = new ExitDialog(getActivity(), R.style.dialog);
        dialog.setOnConfirmClickListener(new ExitDialog.OnConfirmClickListener() {
            @Override
            public void onConfirmClick() {
                confirmLogout();
            }
        });
        dialog.show();
    }

    /**
     * 用户详情
     */
    private void getUserDetail() {
        GsonRequestHelper helper = new GsonRequestHelper(getActivity());
        Map<String, String> params = new HashMap<>();
        String getUrl = ApiDefine.USER_INFO;
        helper.sendPOSTRequest(getUrl, UserBean.class, params, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    UserBean bean = (UserBean) data;
                    SPHelper.setUid(bean.userId);
                    textName.setText(bean.userName);

                    String imageUrl = ApiDefine.IMAGE_URL_BASE + bean.avatar;

                    RequestOptions options = new RequestOptions()
                            .placeholder(R.mipmap.image_default);

                    ImageLoader.getInstance()
                            .displayImage(imageUrl, user_avatar, LinhuApp.options);
                }
            }
        });
    }

    /**
     * 登出
     */
    private void confirmLogout() {
        SPHelper.setAuthToken("");
        SPHelper.setPassword("");

        Intent intent = new Intent(getActivity(), LoginActivity.class);
        intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TASK | intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

//        GsonRequestHelper helper = new GsonRequestHelper(getActivity());
//        Map<String, String> params = new HashMap<>();
//        String getUrl = ApiDefine.LOGOUT;
//        helper.sendPOSTRequest(getUrl, BaseBean.class, params, false, null);
//        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
//            @Override
//            public void onError(VolleyError error) {
//
//            }
//
//            @Override
//            public void onSuccess(Object data) {
//                if (data != null) {
//                    BaseBean bean = (BaseBean) data;
//                    if (((BaseBean) data).code.equals("0")) {
//                        SPHelper.setAuthToken("");
//                        SPHelper.setPassword("");
//
//                        Intent intent = new Intent(getActivity(), LoginActivity.class);
//                        intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TASK | intent.FLAG_ACTIVITY_NEW_TASK);
//                        startActivity(intent);
//                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
//                        IntentUtils.skipToLogin(getActivity());
//                    } else {
//                        ToastUtils.showText(getActivity(), bean.msg);
//                    }
//                }
//            }
//        });
    }
}
